package com.ruoyi.channelApi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.channelApi.domain.TChannelPush;
import com.ruoyi.channelApi.domain.bo.TChannelPushBo;
import com.ruoyi.channelApi.domain.vo.TChannelPushVo;
import com.ruoyi.channelApi.mapper.TChannelPushMapper;
import com.ruoyi.channelApi.service.ITChannelPushService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 渠道推送记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-30
 */
@RequiredArgsConstructor
@Service
public class TChannelPushServiceImpl implements ITChannelPushService {

    private final TChannelPushMapper baseMapper;

    /**
     * 查询渠道推送记录
     */
    @Override
    public TChannelPushVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询渠道推送记录列表
     */
    @Override
    public TableDataInfo<TChannelPushVo> queryPageList(TChannelPushBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TChannelPush> lqw = buildQueryWrapper(bo);
        Page<TChannelPushVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询渠道推送记录列表
     */
    @Override
    public List<TChannelPushVo> queryList(TChannelPushBo bo) {
        LambdaQueryWrapper<TChannelPush> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TChannelPush> buildQueryWrapper(TChannelPushBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TChannelPush> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getChannelName()), TChannelPush::getChannelName, bo.getChannelName());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelSource()), TChannelPush::getChannelSource, bo.getChannelSource());
        lqw.eq(StringUtils.isNotBlank(bo.getDataInfo()), TChannelPush::getDataInfo, bo.getDataInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), TChannelPush::getTel, bo.getTel());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
                TChannelPush::getPushTime, params.get("beginTime"), params.get("endTime"));
        lqw.eq(bo.getPushTime() != null, TChannelPush::getPushTime, bo.getPushTime());
        return lqw;
    }

    /**
     * 新增渠道推送记录
     */
    @Override
    public Boolean insertByBo(TChannelPushBo bo) {
        TChannelPush add = BeanUtil.toBean(bo, TChannelPush.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改渠道推送记录
     */
    @Override
    public Boolean updateByBo(TChannelPushBo bo) {
        TChannelPush update = BeanUtil.toBean(bo, TChannelPush.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TChannelPush entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除渠道推送记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
